package com.wumz.spring.write.v2.framework.webmvc;

import java.io.File;

/**
 * Created by wumz on 2020/9/29 11:34
 */
public class MyViewResolver {

    private final String DEFAULT_TEMPLATE_SUFFIX = ".html";
    private File tempateRootDir;

    public MyViewResolver(String templateRoot) {
        tempateRootDir = new File(templateRoot);
    }

    public MyView resolveViewName(String viewName){
        if(null == viewName || "".equals(viewName.trim())){return null;}
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFIX)? viewName : (viewName + DEFAULT_TEMPLATE_SUFFIX);
        File templateFile = new File((tempateRootDir.getPath() + "/" + viewName).replaceAll("/+","/"));
        return new MyView(templateFile);
    }
}
